#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ClearDiagInfo_0x14_test.py
@Time    :   2022/08/29 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
import Set_up 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

ClearDtcInfo_NRC13 = [([0x14],[0x7F,0x14,0x13]),                  
                        ([0x14,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0xFF,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0xFF,0xFF,0xFF,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0x11,0x22,0xFF,0xFF],[0x7F,0x14,0x13]),
                     ]
ClearDtcInfo_NRC31 = [  ([0x14,0xFF,0xFF,0xFE],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x29,0x87],[0x7F,0x14,0x31]),  
                        ([0x14,0x5C,0x11,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x12,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x13,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x14,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x15,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x16,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x01,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x17,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x18,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x19,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x84,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x40,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1A,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x00,0x88],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x01,0x88],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x02,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x03,0x17],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x04,0x16],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x05,0x17],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x06,0x16],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x07,0x54],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x08,0x54],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x09,0x4B],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0A,0x4B],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0B,0x02],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0C,0x02],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0D,0x44],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0E,0x44],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0F,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x10,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1B,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1C,0x4A],[0x7F,0x14,0x31])
                     ]


class Test_ClearDiagInfo_Phy(object):
      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test in default session with physical addressing')
      @allure.description('Test positive response of 0x14 in default session with physical addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_Protocol_TC000371_Default_Phy(self,action,Driver):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 1, iStep, False)

      @allure.severity('normal')
      @allure.description('Test positive response of 0x14 in extended session with physical addressing')
      def test_Service_0x14_Protocol_TC000371_Extended_Phy(self,action,Driver,extend_session):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False)

     
      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in default session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13,ids = ids)
      def test_Service_0x14_NRC13_TC000373_Default_Phy(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in extended session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13,ids = ids)
      def test_Service_0x14_NRC13_TC000373_Extended_Phy(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

      
      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC31]
      @allure.severity('minor')
      @allure.description('Test NRC31 of 0x14 in default session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31,ids = ids)
      def test_Service_0x14_NRC31_TC000374_Default_Phy(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC31]
      @allure.severity('minor')
      @allure.description('Test NRC31 of 0x14 in extended session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31,ids = ids)
      def test_Service_0x14_NRC31_TC000374_Extended_Phy(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

      @allure.severity('minor')
      @allure.description('Test NRC11 of 0x14 in programming session with physical addressing')
      def test_ClearDiagInfo_NRC11_Programming_Phy(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F,0x14,0x11], 2, iStep, False)

      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test NRC22 in default session with physical addressing')
      @allure.description('Test NRC22 of 0x14 in default session with physical addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_ClearDiagnosticInformation_Precondition_TC000372_Default_Phy(self, action, Driver):
         iStep = 1
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 10)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F, 0x14, 0x22], 0, iStep, False)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, False)
         time.sleep(10)
         logging.info("Wait 10s")
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x01], [0x50,0x01,0x00,0x32,0x00,0xC8], 0, iStep, False)
         time.sleep(1)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, True)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)

      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test NRC22 in extended session with physical addressing')
      @allure.description('Test NRC22 of 0x14 in default session with physical addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_ClearDiagnosticInformation_Precondition_TC000372_Extended_Phy(self, action, Driver):
         iStep = 1
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 10)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F, 0x14, 0x22], 0, iStep, False)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, False)
         time.sleep(10)
         logging.info("Wait 10s")
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x03], [0x50,0x03,0x00,0x32,0x00,0xC8], 0, iStep, False)
         time.sleep(1)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, True)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)




class Test_ClearDiagInfo_Fun(object):
      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test in default session with function addressing')
      @allure.description('Test positive response of 0x14 in default session with function addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_Protocol_TC000371_Default_Fun(self,action,Driver):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 1, iStep, False, 'UDSFun', 'noClearFault')

      @allure.severity('normal')
      @allure.description('Test positive response of 0x14 in extended session with function addressing')
      def test_Service_0x14_Protocol_TC000371_Extended_Fun(self,action,Driver):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False, 'UDSFun', 'noClearFault')

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in default session with functional addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13,ids = ids)
      def test_Service_0x14_NRC13_TC000373_Default_Fun(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in extended session with functional addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13,ids = ids)
      def test_Service_0x14_NRC13_TC000373_Extended_Fun(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

      
      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC31]
      @allure.severity('minor')
      @allure.description('Test NRC31 of 0x14 in default session with functional addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31,ids = ids)
      def test_Service_0x14_NRC31_TC000374_Default_Fun(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC31]
      @allure.severity('minor')
      @allure.description('Test NRC31 of 0x14 in extended session with functional addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31,ids = ids)
      def test_Service_0x14_NRC31_TC000374_Extended_Fun(self,action,Driver,req,resp):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

      @allure.severity('minor')
      @allure.description('Test NRC11 of 0x14 in programming session with functional addressing')
      def test_ClearDiagInfo_NRC11_Programming_Fun(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F,0x14,0x11], 2, iStep, False, 'UDSFun', 'noClearFault')

      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test NRC22 in default session with functional addressing')
      @allure.description('Test NRC22 of 0x14 in default session with functional addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_ClearDiagnosticInformation_Precondition_TC000372_Default_Fun(self, action, Driver):
         iStep = 1
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False, 'UDSFun', 'noClearFault')
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 10)
         time.sleep(3)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F, 0x14, 0x22], 0, iStep, False, 'UDSFun', 'noClearFault')
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, False)
         time.sleep(10)
         logging.info("Wait 10s")
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False, 'UDSFun', 'noClearFault')
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x01], [0x50,0x01,0x00,0x32,0x00,0xC8], 0, iStep, False, 'UDSFun', 'noClearFault')
         time.sleep(1)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, True)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)

      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test NRC22 in extended session with functional addressing')
      @allure.description('Test NRC22 of 0x14 in default session with functional addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_Service_0x14_ClearDiagnosticInformation_Precondition_TC000372_Extended_Fun(self, action, Driver):
         iStep = 1
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False, 'UDSFun', 'noClearFault')
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 10)
         time.sleep(1)
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F, 0x14, 0x22], 0, iStep, False, 'UDSFun', 'noClearFault')
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, False)
         time.sleep(10)
         logging.info("Wait 10s")
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False, 'UDSFun', 'noClearFault')
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x03], [0x50,0x03,0x00,0x32,0x00,0xC8], 0, iStep, False, 'UDSFun', 'noClearFault')
         time.sleep(1)
         iStep = Driver['CAN'].SetVehicleSpeedStatus(Driver, iStep, True)
         iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 2)
     